package com.usf.jprovider.schema.provider.file.formated;

import java.io.FileNotFoundException;
import java.io.IOException;

import javax.xml.bind.annotation.XmlAttribute;

import com.usf.jprovider.exception.PropertyNotFoundException;
import com.usf.jprovider.object.JObjectNode;
import com.usf.jprovider.object.env.Environment;
import com.usf.jprovider.object.type.JType;
import com.usf.jprovider.tools.PropertiesTools;

public final class PropertiesFile extends AbstractFormatedFileSource{

	@XmlAttribute protected String property;
	
	public PropertiesFile() {
		super();
		property = "";
	}
	public String getProperty() {
		return property;
	}

	@Override
	protected String createUnformated(Environment env, JObjectNode node) throws Exception {
	    String values = (String)cacheValues(env, node);
	    return valueSelector.create(env, node, values);
	}
	@Override
	public boolean checkValue(Environment env, JObjectNode node, JType<?> value) throws Exception {
	    String values = (String)cacheValues(env, node);
	    return valueSelector.check(env, node, values, value.toString());
	}
	@Override
	public Object values(Environment env) throws FileNotFoundException, IOException, PropertyNotFoundException {
		String result = PropertiesTools.loadFile(env.fullPath(path)).getProperty(property);
		if(result == null) throw new PropertyNotFoundException(property, path);
		return result;
	}
	
	@Override
	protected String cacheIdentifier() {
		return property + "-" + path;
	}
}
